﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;

namespace RemoteDesktop
{
    /// <summary>
    /// ShareWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ShareWindow : Window
    {
        private readonly List<WindowPreview> previews = null;
        private readonly MainWindow mWindow = null;
        private WindowPreview selectedItem = null;
        public ShareWindow(MainWindow mWin)
        {
            InitializeComponent();
            mWindow = mWin;
            previews = new List<WindowPreview>();
            // 现在你有了所有已打开窗口的句柄列表
            GetRunningProcessNames();
            myListView.ItemsSource = previews;
        }
        private void GetRunningProcessNames()
        {
            IntPtr deskHandle = Desktopduplication.GetDesktopWindow();
            if (deskHandle != IntPtr.Zero)
            {
                Bitmap bit = Desktopduplication.CaptureWindow(deskHandle);
                WindowPreview wp = new WindowPreview
                {
                    ImagePath = ImgUtils.ConvertBitmapToBitmapSource(bit),
                    Name = "桌面",
                    WindowHandle = deskHandle
                };
                previews.Add(wp);
            }
            // 获取当前运行的所有进程
            Process[] processes = Process.GetProcesses();

            // 筛选出桌面环境的进程
            foreach (Process process in processes)
            {
                try
                {
                    // 尝试获取进程的主窗口句柄
                    IntPtr windowHandle = process.MainWindowHandle;
                    if (windowHandle != IntPtr.Zero)
                    {
                        // 获取进程名称
                        Bitmap bit = Desktopduplication.CaptureWindow(windowHandle);
                        WindowPreview wp = new WindowPreview();
                        wp.ImagePath = ImgUtils.ConvertBitmapToBitmapSource(bit);
                        wp.Name = process.ProcessName;
                        wp.WindowHandle = windowHandle;
                        previews.Add(wp);
                    }
                }
                catch (Exception ex)
                {
                    // 处理可能发生的异常，例如访问被拒绝
                    Console.WriteLine(ex.Message);
                }
            }
        }
        private void myListView_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            // 获取选中的ListBoxItem
            selectedItem = (WindowPreview)myListView.SelectedItem;
            //mWindow.currHandle = selectedItem.WindowHandle;
            text.Content = selectedItem.Name;
        }
        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
            if (selectedItem == null)
            {
                System.Windows.MessageBox.Show("请选择窗口");
                return;
            }
            mWindow.ShareWindow(selectedItem);
            Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        // 导入用于枚举窗口的API
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

        // 定义回调函数的委托
        private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

        // 实现回调函数
        private static bool EnumWindow(IntPtr hWnd, IntPtr lParam)
        {
            GCHandle gch = GCHandle.FromIntPtr(lParam);
            List<IntPtr> list = gch.Target as List<IntPtr>;
            if (list == null)
                throw new InvalidCastException("GCHandle cannot be casted");

            list.Add(hWnd);
            return true;
        }

        private static List<IntPtr> EnumWindows()
        {
            List<IntPtr> windowHandles = new List<IntPtr>();
            GCHandle listHandle = GCHandle.Alloc(windowHandles);
            try
            {
                EnumWindowsProc enumProc = EnumWindow;
                EnumWindows(enumProc, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                if (listHandle.IsAllocated)
                {
                    listHandle.Free();
                }
            }
            return windowHandles;
        }
    }
}
